package com.tutou.ikgroup.mapper;

import com.tutou.ikgroup.pojo.dto.ChatDTO;
import com.tutou.ikgroup.pojo.po.Chatroom;
import com.tutou.ikgroup.pojo.po.Friend;
import com.tutou.ikgroup.pojo.po.Message;
import com.tutou.ikuser.pojo.po.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChatMapper {

    @Insert("insert into chatrooms (created_by, room_name) value (#{senderId},#{chatroom.roomName})")
    @Options(useGeneratedKeys = true, keyProperty = "chatroom.chatroomId", keyColumn = "chat_room_id")
    void crateChatroom(ChatDTO chatDTO);

    @Select("select chat_room_id from chatrooms where room_name=#{roomName}")
    String getChatroomId(String roomName);

    @Select("select room_name from chatrooms where chat_room_id=#{chatroomId}")
    String getChatroomName(String chatroomId);

    void saveMessage(ChatDTO chatDTO);

    @Select("select chat_room_id from chatroommembers where user_id=#{userId}")
    List<String> getChatroomIdListByUserId(String userId);

    //群主加入聊群
    @Insert("insert into chatroommembers (chat_room_id,user_id) values (#{chatroom.chatroomId},#{senderId})")
    void CsaveChatroomMembers(ChatDTO chatDTO);

    //邀请加入群聊
    @Insert("insert into chatroommembers (chat_room_id,user_id) values (#{chatroom.chatroomId},#{receiverId})")
    void IsaveChatroomMembers(ChatDTO chatDTO);

    //单聊信息
    List<Message> findOBOMessage(ChatDTO chatDTO);

    //群聊信息
    List<Message> findOBMMessage(ChatDTO chatDTO);

    int existUserInChatroom(ChatDTO chatDTO);

    @Delete("delete from chatroommembers where chat_room_id=#{chatroomId}")
    void deleteChatroomMembers(String chatroomId);

    @Delete("delete from chatrooms where chat_room_id=#{chatroomId}")
    void deleteChatroom(String chatroomId);

    @Delete("delete from chatroommembers where chat_room_id=#{chatroom.chatroomId} and user_id=#{senderId}")
    void exitChatroom(ChatDTO chatDTO);

    @Delete("delete from messages where chat_room_id=#{chatroom.chatroomId}")
    void deleteChatroomMessage(String chatroomId);

    List<User> getChatroomMember(String chatroomId);

    @Select("select count(*)  from chatrooms where room_name=#{chatRoomName}")
    int existChatroom(String chatRoomName);
}
